package com.roc.admin.interfaces.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.roc.admin.application.service.UserService;
import com.roc.admin.domain.entity.User;
import com.roc.admin.domain.valueobject.UserPermission;
import com.roc.admin.interfaces.common.Result;
import com.roc.admin.interfaces.dto.UserDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 用户控制器
 */
@Tag(name = "用户管理", description = "用户管理相关接口")
@RestController
@RequestMapping("/user")
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @Operation(summary = "创建用户")
    @PostMapping
    public Result<Long> createUser(@Validated @RequestBody UserDTO userDTO) {
        Long userId = userService.createUser(userDTO);
        return Result.success(userId);
    }

    @Operation(summary = "更新用户")
    @PutMapping
    public Result<Boolean> updateUser(@Validated @RequestBody UserDTO userDTO) {
        boolean success = userService.updateUser(userDTO);
        return Result.success(success);
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("/{userId}")
    public Result<Boolean> deleteUser(@Parameter(description = "用户ID") @PathVariable Long userId) {
        boolean success = userService.deleteUser(userId);
        return Result.success(success);
    }

    @Operation(summary = "获取用户信息")
    @GetMapping("/{userId}")
    public Result<UserDTO> getUserById(@Parameter(description = "用户ID") @PathVariable Long userId) {
        UserDTO userDTO = userService.getUserById(userId);
        return Result.success(userDTO);
    }

    @Operation(summary = "根据用户名获取用户信息")
    @GetMapping("/username/{username}")
    public Result<UserDTO> getUserByUsername(@Parameter(description = "用户名") @PathVariable String username) {
        UserDTO userDTO = userService.getUserByUsername(username);
        return Result.success(userDTO);
    }

    @Operation(summary = "获取用户列表")
    @GetMapping("/list")
    public Result<List<UserDTO>> getUserList() {
        List<UserDTO> userDTOList = userService.getUserList();
        return Result.success(userDTOList);
    }

    @Operation(summary = "根据部门ID获取用户列表")
    @GetMapping("/list/dept/{deptId}")
    public Result<List<UserDTO>> getUserListByDeptId(@Parameter(description = "部门ID") @PathVariable Long deptId) {
        List<UserDTO> userDTOList = userService.getUserListByDeptId(deptId);
        return Result.success(userDTOList);
    }

    @Operation(summary = "修改用户密码")
    @PutMapping("/password")
    public Result<Boolean> changePassword(@Parameter(description = "用户ID") @RequestParam Long userId,
                                         @Parameter(description = "旧密码") @RequestParam String oldPassword,
                                         @Parameter(description = "新密码") @RequestParam String newPassword) {
        boolean success = userService.changePassword(userId, oldPassword, newPassword);
        return Result.success(success);
    }

    @Operation(summary = "重置用户密码")
    @PutMapping("/password/reset")
    public Result<Boolean> resetPassword(@Parameter(description = "用户ID") @RequestParam Long userId,
                                        @Parameter(description = "新密码") @RequestParam String newPassword) {
        boolean success = userService.resetPassword(userId, newPassword);
        return Result.success(success);
    }

    @Operation(summary = "获取用户权限列表")
    @GetMapping("/permissions/{userId}")
    public Result<Set<UserPermission>> getUserPermissions(@Parameter(description = "用户ID") @PathVariable Long userId) {
        Set<UserPermission> permissions = userService.getUserPermissions(userId);
        return Result.success(permissions);
    }

    @Operation(summary = "分配用户角色")
    @PostMapping("/role")
    public Result<Boolean> assignRoles(@Parameter(description = "用户ID") @RequestParam Long userId,
                                      @Parameter(description = "角色ID列表") @RequestParam List<Long> roleIds) {
        boolean success = userService.assignRoles(userId, roleIds);
        return Result.success(success);
    }

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result<UserDTO> login(@Parameter(description = "用户名") @RequestParam String username,
                                @Parameter(description = "密码") @RequestParam String password) {
        User user = userService.login(username, password);
        if (user == null) {
            return Result.failed("用户名或密码错误");
        }
        
        // 使用Sa-Token登录
        StpUtil.login(user.getId());
        
        UserDTO userDTO = userService.getUserByUsername(username);
        return Result.success(userDTO);
    }
    
    @Operation(summary = "用户登出")
    @PostMapping("/logout")
    public Result<String> logout() {
        StpUtil.logout();
        return Result.success("登出成功");
    }
    
    @Operation(summary = "获取当前登录用户信息")
    @GetMapping("/info")
    public Result<UserDTO> getCurrentUserInfo() {
        // 获取当前登录用户ID
        Long userId = StpUtil.getLoginIdAsLong();
        UserDTO userDTO = userService.getUserById(userId);
        return Result.success(userDTO);
    }
    
    @Operation(summary = "检查用户是否登录")
    @GetMapping("/isLogin")
    public Result<Boolean> isLogin() {
        return Result.success(StpUtil.isLogin());
    }
}
